package databases

import (
	"fmt"
	"ginchat/internal/globals/vipers"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var mysqlClient *gorm.DB

func InitMySQLClient() (err error) {
	v := vipers.GetDatabaseViper()
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		v.GetString("mysql.user"),
		v.GetString("mysql.password"),
		v.GetString("mysql.host"),
		v.GetInt("mysql.port"),
		v.GetString("mysql.database"),
	)
	mysqlClient, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	return err
}

func GetMySQLClient() *gorm.DB {
	return mysqlClient
}
